package com.neutech.tank;

import com.neutech.Util.ImageUtils;
import com.neutech.base.Direction;
import com.neutech.base.Drawable;
import com.neutech.base.Moveable;
import com.neutech.base.Sprite;
import com.neutech.constant.Constant;
import com.neutech.frame.TankFrame;

import java.awt.*;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

public class EnemyTank extends Sprite implements Drawable, Moveable {

    private int level = 1;
    private Map<String,Image> imgMap = new HashMap<>();
    public List<Bullet> bulletList = new CopyOnWriteArrayList<>();

    public Direction direction = Direction.DOWN;
    private int index = 1;
    private int speed = 1;
    private EnemyTank enemyTank;
    private boolean fire;
    public boolean move1 =true;//TODO 声明的移动
    private int bulletsize = 1;
    public Random random = new Random();
    /**
     * 下面的是使用数字记录时间，每次刷新为10ms，刷新100次就是1秒
     */
    private int currentTime = 0;
//    private long currentTimeMillis = System.currentTimeMillis();

    public EnemyTank() {
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public EnemyTank(int x, int y, int width, int height ) {
        super(x, y, width, height);

        loadingImg();
    }

    @Override
    public void move() {
//        if (System.currentTimeMillis() - currentTimeMillis > 3000){
//            currentTimeMillis = System.currentTimeMillis();
//        }
        currentTime++;
        if (currentTime >= 300){
           int randomNum = random.nextInt(4);
            if (randomNum == 0){
                if (move1 ){direction = Direction.UP;}//TODO 方向

            }
            if (randomNum == 1){
                if (move1){ direction = Direction.RIGHT;}

            }
            if (randomNum == 2){
                if (move1){direction = Direction.DOWN;}

            }
            if (randomNum == 3){
                if (move1){direction = Direction.LEFT;}

            }
            currentTime = 0;
        }
        if (direction == Direction.UP){
            setY(getY()-speed);
        }
        if (direction == Direction.RIGHT){
            setX(getX()+speed);
        }
        if (direction == Direction.DOWN){
            setY(getY()+speed);
        }
        if (direction == Direction.LEFT){
             setX(getX()-speed);
        }


    }
//     碰撞监测
    @Override
    public Rectangle getRectangle() {
        return new  Rectangle(getX(),getY(),getWidth(),getHeight());
    }


    @Override
    public void draw(Graphics g) {
        /**
         * 图片切换
         * 如果觉得快，可以写减速器就是下面这样的
         * index+=0.2；
         * Math.floor(index);向下取整
         * 把Math.floor(index)放到index++那个地方就可以起到减速器的效果
         */
        if (direction == Direction.UP){
            g.drawImage(imgMap.get("top" + index++),getX(),getY(),null);
        }
        if (direction == Direction.RIGHT){
            g.drawImage(imgMap.get("right" + index++),getX(),getY(),null);
        }
        if (direction == Direction.DOWN){
            g.drawImage(imgMap.get("down" + index++),getX(),getY(),null);
        }
        if (direction == Direction.LEFT){
            g.drawImage(imgMap.get("left" + index++),getX(),getY(),null);
        }
        if (index > 2 ){
            index = 1;

        }
        move();
        fire();
        outOfBounds();

    }
    public void loadingImg(){
        if (level ==1){

            imgMap.put("top1", ImageUtils.getValue("tank_w_1_1"));
            imgMap.put("top2",ImageUtils.getValue("tank_w_1_2"));
            imgMap.put("right1",ImageUtils.getValue("tank_w_1_3"));
            imgMap.put("right2",ImageUtils.getValue("tank_w_1_4"));
            imgMap.put("down1",ImageUtils.getValue("tank_w_1_5"));
            imgMap.put("down2",ImageUtils.getValue("tank_w_1_6"));
            imgMap.put("left1",ImageUtils.getValue("tank_w_1_7"));
            imgMap.put("left2",ImageUtils.getValue("tank_w_1_8"));

        }
    }

    /**
     * 碰撞改变
     */
    public void changeDir(){
        int randomNum = random.nextInt(4);
        if (randomNum == 0){
            direction = Direction.UP;
        }
        if (randomNum == 1){
            direction = Direction.RIGHT;
        }
        if (randomNum == 2){
            direction = Direction.DOWN;
        }
        if (randomNum == 3){
            direction = Direction.LEFT;
        }
    }

    /**
     * 边界阻碍
     */
    public void outOfBounds(){
        if (direction == Direction.UP){
            /**
             *  x代表左右，左面是0，右面是正无穷大，y上面是0，下面是正无穷大
             */
            if (getY() <= Constant.MARGIN_TOP){
                setY(Constant.MARGIN_TOP);
                changeDir();
            }
        }
        if (direction == Direction.RIGHT){
            if (getX() >= Constant.BLACK_SIZE){
                setX(Constant.BLACK_SIZE);
                changeDir();
            }

        }
        if (direction == Direction.DOWN){
            if (getY() >=Constant.BLACK_SIZE){
                setY(Constant.BLACK_SIZE);
                changeDir();
            }
        }
        if (direction == Direction.LEFT){
            if (getX()<= Constant.MARGIN_LEFT){
                setX(Constant.MARGIN_LEFT);
                changeDir();

            }
        }

    }

    /**
     * 发射子弹的行为
     */
    public void fire(){
        if (bulletList.size() == 0 && random.nextInt(100)>98 ){
            /**
             * 给子弹获取范围
             */
//            bulletList.add(new Bullet());
            if (direction == Direction.UP){
                bulletList.add(new Bullet(
                        (Constant.SPRITE_SIZE/*用坦克自己的宽度除以2*//2 + getX())-Constant.BIG_BULLET_SIZE/2,
                        getY()-Constant.BIG_BULLET_SIZE,Constant.BIG_BULLET_SIZE,
                        34,5,direction/*给子弹方向*/,this));
            }
            if (direction == Direction.RIGHT){
                bulletList.add(new Bullet(
                        getX()+Constant.BIG_BULLET_SIZE,
                        getY()+Constant.SPRITE_SIZE /2-Constant.BIG_BULLET_SIZE/2,
                        Constant.SPRITE_SIZE,Constant.BIG_BULLET_SIZE,5,direction,this/*在里面就是this*/));
            }
            if (direction == Direction.DOWN){
                bulletList.add(new Bullet(
                        (Constant.SPRITE_SIZE/*用坦克自己的宽度除以2*//2 + getX())-Constant.BIG_BULLET_SIZE/2,
                        getY()+34,Constant.BIG_BULLET_SIZE,Constant.BIG_BULLET_SIZE,5,direction,this));

            }
            if (direction == Direction.LEFT){
                bulletList.add(new Bullet(
                        getX()-Constant.BIG_BULLET_SIZE,
                        getY()+Constant.SPRITE_SIZE /2-Constant.BIG_BULLET_SIZE/2,Constant.BIG_BULLET_SIZE,
                        Constant.BIG_BULLET_SIZE,5,direction,this));

            }


        }


    }

}
